package com.example.conf;

import java.util.Map;

import javax.websocket.Session;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.*;
import org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
import org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.OriginHandshakeInterceptor;

import com.example.util.RequestUtil;
import com.example.util.SpringUtil;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * @author changwei
 * @date 2019年11月18日
 */
@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfig2 implements WebSocketConfigurer{

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(echoWebSocketHandler(), "/messages")
				.addInterceptors(new OriginHandshakeInterceptor2()) .withSockJS();
		
	}

	private WebSocketHandler echoWebSocketHandler() {
		
		return new MyEchoWebSocketHandler();
	}

	class OriginHandshakeInterceptor2 extends OriginHandshakeInterceptor{
		
		@Override
		public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
				WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
			// TODO Auto-generated method stub
			attributes.put("bb", "bbbb");
			return super.beforeHandshake(request, response, wsHandler, attributes);
		}
		
	}
	
	class MyEchoWebSocketHandler implements WebSocketHandler{

		@Override
		public void afterConnectionEstablished(WebSocketSession session) throws Exception {
			String socketSessionId = session.getId();
			StandardWebSocketSession sw = (StandardWebSocketSession) session;
			Session s = sw.getNativeSession();
			TextMessage textMessage = new TextMessage("我是发送的测试消息");
			sw.sendMessage(textMessage);
//			String httpSessionId = RequestUtil.getSession().getId();
			Map<String, Object> a = session.getAttributes();
			log.debug("**** afterConnectionEstablished method excute ****");
			//SimpMessagingTemplate template = SpringUtil.getBean(SimpMessagingTemplate.class);
			//template.convertAndSend("afterConnectionEstablished method excute");
		}

		@Override
		public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
			log.debug("**** handleMessage method excute ****, 我接收到前端发送消息:{}", message.getPayload());

		}

		@Override
		public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
			log.debug("**** handleTransportError method excute ****");
		}

		@Override
		public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
			log.debug("**** afterConnectionClosed method excute ****");
		}

		@Override
		public boolean supportsPartialMessages() {
			log.debug("**** supportsPartialMessages method excute ****");
			return false;
		}
		
	}
	
}
